package com.example.springmvcdemo1.controller;

import com.example.springmvcdemo1.enity.Student;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class StudentController {
/*    @GetMapping("/test")
    public String study(String name,int age) {
        return "hello,springmvc,name:" + name + ",age:" + age;
    }*/

/*    @GetMapping({"/test/{name}/{pwd}","/test/{name}"})
    public String study(@PathVariable String name,@PathVariable(required = false,name = "pwd") String password) {
        return "name:" + name + ",password:" + password;
    }*/
/*    @RequestMapping("/upload")

    public Boolean upload(@RequestPart("myimg")MultipartFile file) {
        File file1 = new File("E:\\study\\myimg.jpg");
        try {
            file.transferTo(file1);
            return true;
        }catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }*/
/*@RequestMapping("/upload")
    public Boolean upload(@RequestPart("img")MultipartFile file) {
        //UUID生成随机数更不容易发生重复
        //每次获取的文件名都独一无二，这样就不会出现覆盖。文件名产生：随机生成的随机数（UUID）生成的+文件原来的后缀
        String fileName = UUID.randomUUID() + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        File file1 = new File("E:\\study\\" + fileName);
        try {
            file.transferTo(file1);
            return true;
        }catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }*/


/*    @RequestMapping("/cookie")
    public void test(@CookieValue("bite") String bite) {

    }
    @RequestMapping("/header")
    public void test1(@RequestHeader("User-Agent")String userAgent) {

    }*/

    @RequestMapping("/setse")
    public void setse(HttpServletRequest request) {
        //获取HttpSession对象，参数设置为true，表示如果没有session对象就创建一个session
        HttpSession session = request.getSession(true);
        session.setAttribute("username","java");
    }

    @RequestMapping("/getsess")
    public void getsess(@SessionAttribute(value = "username",required = false) String username) {

    }

    @RequestMapping("/student")
    public Student stu() {
        Student student = new Student();
        return student;
    }

    @RequestMapping("/stu")
    public String stu1(@RequestParam("name") String username,@RequestParam(required = false) Integer age) {
        return "你好，" + username + age;
    }
}
